package com.zlll.winner.service.mall;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.mall.MallSeckillManage;
import com.zlll.winner.business.model.mall.MallSelectGoods;

import com.zlll.winner.business.service.mall.IMallGoodsDetailsService;
import com.zlll.winner.business.vo.mall.MallGoodsDetailsVo;
import com.zlll.winner.business.vo.mall.MallSelectGoodsVo;
import com.zlll.winner.mapper.mall.MallSelectGoodsMapper;
import com.zlll.winner.business.service.mall.IMallSelectGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 精选商品 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-10-08
 */
@Service
public class MallSelectGoodsServiceImpl extends ServiceImpl<MallSelectGoodsMapper, MallSelectGoods> implements IMallSelectGoodsService {

    @Autowired
    private MallSelectGoodsMapper mallSelectGoodsMapper;
    @Autowired
    private IMallGoodsDetailsService mallGoodsDetailsService;

    @Override
    public IPage<MallGoodsDetailsVo> getSelectGoodsPage(String goodsName, String goodsNo, Integer pageSize, Integer pageNo) {
        IPage<MallGoodsDetailsVo> page = new Page<MallGoodsDetailsVo>(pageNo, pageSize);
        List<MallGoodsDetailsVo> list = mallSelectGoodsMapper.getMallGoodsDetailsList(goodsName, goodsNo, page);
        page.setRecords(list);
        return page;
    }

    @Override
    public IPage<MallSelectGoodsVo> getPage(String goodsName, Integer pageSize, Integer pageNo) {
        IPage<MallSelectGoodsVo> page = new Page<MallSelectGoodsVo>(pageNo, pageSize);
        List<MallSelectGoodsVo> list = mallSelectGoodsMapper.getMallSelectGoodsVoPage(goodsName, page);
        page.setRecords(list);
        return page;
    }

    @Override
    public MallSelectGoods add(MallSelectGoods mallSelectGoods) {
        mallSelectGoods.setCreateTime(System.currentTimeMillis());
        mallSelectGoods.setUpdateTime(System.currentTimeMillis());

        mallSelectGoods.setIsDel(0);
        save(mallSelectGoods);
        if (mallSelectGoods.getGoodsStatus() != null && !mallSelectGoods.equals(2)) {
            mallGoodsDetailsService.setPutaway(mallSelectGoods.getGoodsId());
        }
        return mallSelectGoods;
    }

    @Override
    public MallSelectGoodsVo getMallSelectGoodsVoById(Long id) {
        List<MallSelectGoodsVo> list = mallSelectGoodsMapper.getMallSelectGoodsVoById(id);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<MallSelectGoods> isRepeat(Long goodsId) {
        LambdaQueryWrapper<MallSelectGoods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MallSelectGoods::getGoodsId, goodsId);
        queryWrapper.eq(MallSelectGoods::getIsDel, 0);
        return mallSelectGoodsMapper.selectList(queryWrapper);
    }

    @Override
    public MallSelectGoods edit(MallSelectGoods mallSelectGoods) {
        mallSelectGoods.setUpdateTime(System.currentTimeMillis());
        if (mallSelectGoods.getStatus().equals(0)) {
            mallSelectGoods.setStatus(null);
        }
        updateById(mallSelectGoods);

        if (mallSelectGoods.getGoodsStatus() != null && !mallSelectGoods.equals(2)) {
            mallGoodsDetailsService.setPutaway(mallSelectGoods.getGoodsId());
        }
        return mallSelectGoods;
    }
}
